/**
 * Helper Class for tree manipulation
 * @class Footbar.TreeUtil
 * @static
 */
if (typeof Trucoder == "undefined") {
    Trucoder = {};
}

Trucoder.GridUtil={
  parseXML2KeywordsForGrid:function(gridname){
        var xmlObject=Footbar.Plugins.getActiveEngine().retriveKeywordsXML();
        if(xmlObject){
            if(typeof(xmlObject)=='string'){
                xmldata = (new DOMParser()).parseFromString(xmlObject, "text/xml");
            }else{
                xmldata=xmlObject;
            }
        }else{
            alert("Unable to retive XML Object in Trucoder.GridUtil.parseXML2KeywordsForGrid");
        }

        var root = xmldata.getElementsByTagName("root")[0];
        var items = root.getElementsByTagName("pair");
        var messages = root.getElementsByTagName("message");

        if(messages!=null && messages.length>0){
            alert(messages[0].getAttribute("value"));
            return;
        }
        //Check if there is message attached to the xml doc
        var grid=document.getElementById( gridname );
        var gridRow = grid.getElementsByTagName('rows')[0];
        //remove previous checkboxes
        try {
            // This is one way to remove all children from a node
            while (gridRow.firstChild)
            {
                //The list is LIVE so it will re-index each call
                gridRow.removeChild(gridRow.firstChild);
            };
        } catch(e) {
            alert(e)
        }

        var trow = document.createElement("row");
        for (var i=0;i<items.length;i++){
            if(i%3 == 0){
                trow = document.createElement("row");
                gridRow.appendChild(trow);
            }
            var set=items[i];
            var checkbox = document.createElement("checkbox");
            checkbox.setAttribute("name",gridname+"-tags")
            checkbox.setAttribute("label", set.getAttribute("value"));
            checkbox.setAttribute("value", set.getAttribute("key"));
            trow.appendChild(checkbox);
        }
    }

}
